package cn.stylefeng.guns.modular.dms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.dms.entity.Directory;
import cn.stylefeng.guns.modular.dms.mapper.DirectoryMapper;
import cn.stylefeng.guns.modular.dms.model.params.DirectoryParam;
import cn.stylefeng.guns.modular.dms.model.result.DirectoryResult;
import  cn.stylefeng.guns.modular.dms.service.DirectoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件目录 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-09
 */
@Service
public class DirectoryServiceImpl extends ServiceImpl<DirectoryMapper, Directory> implements DirectoryService {

    @Override
    public void add(DirectoryParam param){
        Directory entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DirectoryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DirectoryParam param){
        Directory oldEntity = getOldEntity(param);
        Directory newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DirectoryResult findBySpec(DirectoryParam param){
        return null;
    }

    @Override
    public List<DirectoryResult> findListBySpec(DirectoryParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(DirectoryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DirectoryParam param){
        return param.getDirectoryId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Directory getOldEntity(DirectoryParam param) {
        return this.getById(getKey(param));
    }

    private Directory getEntity(DirectoryParam param) {
        Directory entity = new Directory();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
